<template>
  <el-row class="tree-tools" style="width:100%">
    <el-col class="left" :span="18">
      <i class="el-icon-s-home"></i>
      <span>{{ item.name }}</span>
    </el-col>
    <el-col class="right" :span="6">
      <span>{{ item.manager }}</span>
      <el-dropdown @command="mycommand">
        <span class="el-dropdown-link">
          操作
          <i class="el-icon-arrow-down el-icon--right"></i>
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item command="add">添加子部门</el-dropdown-item>
          <el-dropdown-item v-if="!item.isHead" command="edit">查看部门</el-dropdown-item>
          <el-dropdown-item v-if="!item.isHead" command="del">删除部门</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </el-col>
  </el-row>
</template>


<script>
import { delDeptApi } from "@/api/departments";
export default {
  // item 从外界传入的数据
  props: ["item"],
  methods: {
    mycommand(command) {
      if (command === "add") {
        // add
        this.$emit("addShow", this.item.id);
      } else if (command === "edit") {
        // edit
        this.$emit("editShow", this.item.id);
      } else if (command === "del") {
        // 弹窗确认
        this.$confirm("此操作将永久删除该文件, 是否继续?", "警告")
          .then(async () => {
            // 发送请求，提交删除的数据
            // console.log(this.item.id);
            await delDeptApi(this.item.id);
            // 提示成功
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            // 更新数据
            this.$emit("updateData");
            // console.log(true);
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除"
            });
          });
      }
    }
  }
};
</script>


<style lang="scss">
.tree-tools {
  .left {
    font-weight: 700;
    i {
      margin-right: 8px;
    }
  }
  .right {
    text-align: right;
    span {
      margin-right: 20px;
    }
    .el-dropdown-link {
      font-size: 16px;
    }
  }
}
</style>